﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(OpponentFixturePlayerMetaData))]
    public partial class OpponentFixturePlayer
    {
        CricketClubEntities db = new CricketClubEntities();

        public void UpdateOpponentFixturePlayerRecords(FormCollection scorecard, Fixture currentFixture)
        {
            string[] formCollectionOpntPlayerPlayerNo = scorecard.GetValues("item.op_player_no");
            string[] formCollectionOpntPlayerName = scorecard.GetValues("item.op_player_name");
            string[] formCollectionOpntPlayerBatsmanNo = scorecard.GetValues("item.op_batsman_number");
            string[] formCollectionOpntPlayerDismissalId = scorecard.GetValues("op_dismissal_id");
            string[] formCollectionOpntPlayerFielderId = scorecard.GetValues("fielder_id");
            string[] formCollectionOpntPlayerBowlerId = scorecard.GetValues("bowler_id");
            string[] formCollectionOpntPlayerBattingRuns = scorecard.GetValues("item.op_batting_runs");
            string[] formCollectionOpntPlayerBowlerNo = scorecard.GetValues("item.op_bowler_number");
            string[] formCollectionOpntPlayerOvers = scorecard.GetValues("item.op_overs");
            string[] formCollectionOpntPlayerMaidens = scorecard.GetValues("item.op_maidens");
            string[] formCollectionOpntPlayerWickets = scorecard.GetValues("item.op_wickets");
            string[] formCollectionOpntPlayerBowlingRuns = scorecard.GetValues("item.op_bowling_runs");

            for (int i = 0; i < formCollectionOpntPlayerPlayerNo.Length; i++)
            {
                int currentPlayerNo = Int32.Parse(formCollectionOpntPlayerPlayerNo[i]);
                OpponentFixturePlayer currentPlayer = db.OpponentFixturePlayers.Where(cfp => cfp.op_fixture_id == currentFixture.fixture_id && cfp.op_player_no == currentPlayerNo).First();

                if (formCollectionOpntPlayerBatsmanNo[i] != "")
                {
                    currentPlayer.op_batsman_number = Int32.Parse(formCollectionOpntPlayerBatsmanNo[i]);
                }

                if (formCollectionOpntPlayerDismissalId[i] != "")
                {
                    currentPlayer.op_how_out = Int32.Parse(formCollectionOpntPlayerDismissalId[i]);
                }

                if (formCollectionOpntPlayerFielderId[i] != "")
                {
                    currentPlayer.op_fielder_name = Int32.Parse(formCollectionOpntPlayerFielderId[i]);
                }

                if (formCollectionOpntPlayerBowlerId[i] != "")
                {
                    currentPlayer.op_bowler_name = Int32.Parse(formCollectionOpntPlayerBowlerId[i]);
                }

                if (formCollectionOpntPlayerBattingRuns[i] != "")
                {
                    currentPlayer.op_batting_runs = double.Parse(formCollectionOpntPlayerBattingRuns[i]);
                }

                if (formCollectionOpntPlayerBowlerNo[i] != "")
                {
                    currentPlayer.op_bowler_number = Int32.Parse(formCollectionOpntPlayerBowlerNo[i]);
                }

                if (formCollectionOpntPlayerOvers[i] != "")
                {
                    currentPlayer.op_overs = double.Parse(formCollectionOpntPlayerOvers[i]);
                }

                if (formCollectionOpntPlayerMaidens[i] != "")
                {
                    currentPlayer.op_maidens = double.Parse(formCollectionOpntPlayerMaidens[i]);
                }

                if (formCollectionOpntPlayerWickets[i] != "")
                {
                    currentPlayer.op_wickets = double.Parse(formCollectionOpntPlayerWickets[i]);
                }

                if (formCollectionOpntPlayerBowlingRuns[i] != "")
                {
                    currentPlayer.op_bowling_runs = double.Parse(formCollectionOpntPlayerBowlingRuns[i]);
                }

                db.ObjectStateManager.ChangeObjectState(currentPlayer, EntityState.Modified);
                db.SaveChanges();
            }
        }
    }

    public class OpponentFixturePlayerMetaData
    {
        [Required]
        [Display(Name = "Batsman Number:")]
        [Range(1, 11, ErrorMessage = "Batman number must be in range 1 to 11")]
        public object op_batsman_number;

        [Required(ErrorMessage = "You must enter the player name")]
        [Display(Name = "Name:")]
        public object op_player_name;

        [Required]
        [Display(Name = "How Out:")]
        public object op_how_out;

        [Display(Name = "Fielder:")]
        public object op_fielder_name;

        [Display(Name = "Bowler:")]
        public object op_bowler_name;

        [Display(Name = "Runs:")]
        [Range(0, 300, ErrorMessage = "Runs scored must be greater than or equal to zero")]
        public object op_batting_runs;

        [Display(Name = "Bowler Number:")]
        [Range(1, 11, ErrorMessage = "Batman number must be in range 1 to 11")]
        public object op_bowler_number;

        [Display(Name = "Overs:")]
        [Range(0, 300, ErrorMessage = "Runs scored must be greater than or equal to zero")]
        public object op_overs;

        [Display(Name = "Maidens:")]
        [Range(0, 300, ErrorMessage = "Maidens entered must be greater than or equal to zero")]
        public object op_maidens;

        [Display(Name = "Runs:")]
        [Range(0, 300, ErrorMessage = "Runs entered must be greater than or equal to zero")]
        public object op_bowling_runs;

        [Display(Name = "Wickets:")]
        [Range(0, 10, ErrorMessage = "Wickets taken must be in range 1 to 10")]
        public object op_wickets;

    }

}